﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DbfDotNet.Windows.Forms
{
    public class DbfTableView : ListView
    {
        private DbfIndex mIndex;
        private DbfTable mTable;

        public DbfTableView()
        {
            FullRowSelect = true;
            GridLines = true;
            HideSelection = false;
            MultiSelect = false;
            UseCompatibleStateImageBehavior = false;
            View = System.Windows.Forms.View.Details;
            VirtualMode = true;
        }

        private void tableView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {
        }

        protected override void OnColumnClick(ColumnClickEventArgs e)
        {
            base.OnColumnClick(e);
            
            if (e.Column == 0) mIndex = null;
            else SetIndex(mTable.Columns[e.Column]);
            
        }

        ListViewItem theListviewItem;
        
        protected override void OnRetrieveVirtualItem(RetrieveVirtualItemEventArgs e)
        {
            var columnCount = this.Columns.Count;
            try
            {
                DbfRecord record = GetRecordByRow((UInt32)e.ItemIndex);
                if (record == null)
                {
                    theListviewItem.Text = "#null";
                    for (int i = 1; i < columnCount; i++)
                        theListviewItem.SubItems[i].Text = "#null";
                }
                else
                {
                    theListviewItem.Text = "#" + (e.ItemIndex + 1);

                    for (int i = 1; i < columnCount; i++)
                        theListviewItem.SubItems[i].Text = ToString(record.GetField(i));
                }

            }
            catch (Exception)
            {
                //    tableView1.Text = ex.Message;
            }
            e.Item = theListviewItem;

            base.OnRetrieveVirtualItem(e);
        }

        private string ToString(object p)
        {
            if (p == null) return "<null>";
            return p.ToString();
        }

        private DbfRecord GetRecordByRow(uint rowNo)
        {
            DbfRecord record = null;
            if (mIndex == null)
            {
                record = mTable.GetRecord(rowNo);
            }
            else
            {
                record = mIndex.GetRecord(rowNo);
            }
            return record;
        }


        public bool GetRow<T>(uint p, ref T record)
        {
            throw new NotImplementedException();
        }

        public DbfTable DbfTable //(DbfTable<global::DemoApplication1.Individual> individuals)
        {
            get { return mTable; }
            set
            {
                if (mTable == value) return;
                mTable = value;
                Initialize();
            }
        }

        private void SetIndex(DbfDotNet.Core.ColumnDefinition columnDefinition)
        {
            string columnName = (columnDefinition.ColumnName);
            var sortOrder = new DbfDotNet.SortOrder(mTable, false);
            sortOrder.AddField(columnName);
            mIndex = mTable.GetIndex(columnName + ".ndx", sortOrder);
            RefreshGrid();
        }

        public void Initialize()
        {
            Columns.Clear();
            VirtualListSize = 0;
            if (mTable == null) return;
            
            Columns.Add("Record Number", 80);
            theListviewItem = new ListViewItem();
        
            
            for (int i = 1; i < mTable.Columns.Count; i++)
            {
                var c = mTable.Columns[i];
                Columns.Add(c.ColumnName, 120);
                theListviewItem.SubItems.Add("#null");
            }

            VirtualListSize = mTable.RecordCount;
        }

        public void RefreshGrid()
        {
            VirtualListSize = 0;
            VirtualListSize = mTable.RecordCount;
        }

    }
}
